package com.example.bishetianqi;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.bishetianqi.base.BaseActivity;
import com.example.bishetianqi.bean.WeatherBean;
import com.google.gson.Gson;
import com.squareup.picasso.Picasso;

public class SearchCityActivity extends BaseActivity implements View.OnClickListener {

    EditText searchET;
    TextView commitTV,CanceltV;
    GridView hotcityGV;
    String[] hotcitys={"北京","上海","广州","深圳","珠海","佛山","南京","苏州","厦门","南宁","成都","长沙","福州",
            "杭州","武汉","青岛","西安","太原","石家庄","沈阳","重庆","天津",};
    private ArrayAdapter<String> adapter;
    String url1="http://api.map.baidu.com/telematics/v3/weather?location=";
    String url2="&output=json&ak=FkPhtMBK0HTIQNh7gG4cNUttSTyr0nzo";
    String city_search;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if (Build.VERSION.SDK_INT >= 21) {
            View decorView = getWindow().getDecorView();
            decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            getWindow().setStatusBarColor(Color.TRANSPARENT);
        }
        setContentView(R.layout.activity_search_city);
        searchET=findViewById(R.id.Search_ET);
        commitTV=findViewById(R.id.commit_TV);
        CanceltV=findViewById(R.id.Cancel_TV);
        hotcityGV=findViewById(R.id.hotcity_GV);
        commitTV.setOnClickListener(this);
        CanceltV.setOnClickListener(this);

        //设置适配器
        adapter=new ArrayAdapter<>(this,R.layout.item_hotcity,hotcitys);
        hotcityGV.setAdapter(adapter);
        setListener();
    }
        /*设置gridview的监听事件*/
    private void setListener() {
        hotcityGV.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                city_search=hotcitys[position];
                String url=url1+city_search+url2;
                loadData(url);
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.commit_TV:
                city_search=searchET.getText().toString();
                if (!TextUtils.isEmpty(city_search)){
                    //判断输入的城市是否为合法，是否存在
                    String url=url1+city_search+url2;
                    loadData(url);
                }else {
                    Toast.makeText(this, "输入的内容不能为空！", Toast.LENGTH_SHORT).show();
                }
                break;
            case R.id.Cancel_TV:
                finish();
                break;
        }

    }

    @Override
    public void onSuccess(String result) {
        WeatherBean weatherBean = new Gson().fromJson(result, WeatherBean.class);
        if (weatherBean.getError()==0){
            Intent intent=new Intent(this,MainActivity.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK|Intent.FLAG_ACTIVITY_NEW_TASK);
            intent.putExtra("city",city_search);
            startActivity(intent);
        }else {
            Toast.makeText(this, "查询无此城市，判断输入是否合法", Toast.LENGTH_SHORT).show();
        }
    }
}
